#include "stdafx.h"
#include "CHEditCtrl.h"
#include "CHEditView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

UINT WM_FINDREPLACE = ::RegisterWindowMessage(FINDMSGSTRING);

CHEditFindDlg::~CHEditFindDlg ()
{
	if (m_pParent != NULL)
	{
		ASSERT_VALID (m_pParent);

		m_pParent->m_pFindDlg = NULL;
		AfxGetMainWnd()->ModifyStyle(WS_DISABLED,0);
		m_pParent->SetFocus();
	}
}


/////////////////////////////////////////////////////////////////////////////
// CHEditView

#define ID_EDITCTRL 1

IMPLEMENT_DYNCREATE(CHEditView, CView)

DWORD CHEditView::m_dwFindMask = FR_DOWN;
CString CHEditView::m_strFindText;

CHEditView::CHEditView()
{
	m_pWndEditCtrl = NULL;
	m_pFindDlg = NULL;
}

CHEditView::~CHEditView()
{
}

BEGIN_MESSAGE_MAP(CHEditView, CView)
	//{{AFX_MSG_MAP(CHEditView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_WM_SETFOCUS()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
	ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
	ON_COMMAND(ID_EDIT_FIND, OnEditFind)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
	ON_COMMAND(ID_FILE_PRINT, CHEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CHEditView::OnFilePrint)
	// Undo/redo
	ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
	ON_REGISTERED_MESSAGE(WM_FINDREPLACE, OnFindReplace)
	ON_REGISTERED_MESSAGE(CHM_ON_EDITCHANGE, OnEditChange)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHEditView drawing

void CHEditView::OnDraw(CDC* pDC)
{
}

/////////////////////////////////////////////////////////////////////////////
// CHEditView diagnostics

#ifdef _DEBUG
void CHEditView::AssertValid() const
{
	CView::AssertValid();
}

void CHEditView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

void CHEditView::SerializeRaw(CArchive& ar)
{
	ASSERT_VALID(this);

	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->Serialize(ar);
	}

	ASSERT_VALID(this);
}

/////////////////////////////////////////////////////////////////////////////
// CHEditView message handlers

CHEditCtrl* CHEditView::CreateEdit ()
{
	return new CHEditCtrl;
}


int CHEditView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_pWndEditCtrl = CreateEdit ();
	if (m_pWndEditCtrl == NULL)
	{
		TRACE0("CHEditView::OnCreate: edit control is not created\n");
		return -1;
	}

	ASSERT_VALID (m_pWndEditCtrl);
	ASSERT (m_pWndEditCtrl->IsKindOf (RUNTIME_CLASS (CHEditCtrl)));

	if (!m_pWndEditCtrl->Create (WS_CHILD | WS_VISIBLE, 
		CRect (0, 0, 0, 0), this, ID_EDITCTRL))
	{
		TRACE0("CHEditView::OnCreate: cannot create edit control\n");
		return -1;
	}

	return 0;
}

void CHEditView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	if (m_pWndEditCtrl->GetSafeHwnd () != NULL)
	{
		m_pWndEditCtrl->SetWindowPos (NULL, -1, -1, cx, cy,
			SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
	}
}

BOOL CHEditView::OnEraseBkgnd(CDC* /*pDC*/) 
{
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CHEditView printing

BOOL CHEditView::OnPreparePrinting(CPrintInfo* pInfo)
{
	return DoPreparePrinting(pInfo);
}

void CHEditView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->OnBeginPrinting (pDC, pInfo);
	}
}

void CHEditView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->OnEndPrinting (pDC, pInfo);
	}
}

void CHEditView::OnFilePrintPreview() 
{
//	CHPrintPreview (this);
}

void CHEditView::OnEditCut() 
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->Cut();
	}
}

void CHEditView::OnEditCopy() 
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->Copy();
	}
}

void CHEditView::OnEditPaste() 
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->Paste();
	}
}

void CHEditView::OnEditSelectAll() 
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->MakeSelection (CHEditCtrl::ST_ALL_TEXT);
	}
}

void CHEditView::OnUpdateEditCut(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable (m_pWndEditCtrl != NULL && m_pWndEditCtrl->IsCutEnabled());
}

void CHEditView::OnUpdateEditCopy(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable (m_pWndEditCtrl != NULL && m_pWndEditCtrl->IsCopyEnabled());
}

void CHEditView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->OnPrint (pDC, pInfo);
	}
}

void CHEditView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
	if (!pDC->IsPrinting ())
	{
		CView::OnPrepareDC(pDC, pInfo);
	}
}

void CHEditView::OnSetFocus(CWnd* pOldWnd) 
{
	CView::OnSetFocus(pOldWnd);

	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->SetFocus();
	}
}

void CHEditView::OnDestroy() 
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);

		m_pWndEditCtrl->DestroyWindow ();
		delete m_pWndEditCtrl;
		m_pWndEditCtrl = NULL;
	}

	if (m_pFindDlg != NULL)
	{
		m_pFindDlg->PostMessage(WM_CLOSE);
		m_pFindDlg->m_pParent = NULL;
	}

	CView::OnDestroy();
}

void CHEditView::OnEditRedo() 
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->OnRedo();
	}
}

void CHEditView::OnUpdateEditRedo(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable (m_pWndEditCtrl != NULL && m_pWndEditCtrl->CanRedo());
}

void CHEditView::OnEditUndo() 
{
	if (m_pWndEditCtrl != NULL)
	{
		ASSERT_VALID (m_pWndEditCtrl);
		m_pWndEditCtrl->OnUndo();
	}
}

void CHEditView::OnUpdateEditUndo(CCmdUI* pCmdUI) 
{
	ASSERT_VALID (m_pWndEditCtrl);

	pCmdUI->Enable (m_pWndEditCtrl->CanUndo());
}

void CHEditView::OnEditFind()
{
	if (m_pFindDlg != NULL)
	{
		m_pFindDlg->SetFocus ();
		return;
	}

	m_pFindDlg = new CHEditFindDlg;
	m_pFindDlg->m_pParent = this;
	
	if (m_pFindDlg->Create (TRUE, m_strFindText, NULL, m_dwFindMask, this))
	{
		AfxGetMainWnd()->ModifyStyle(0, WS_DISABLED);
	}
	else
	{
		delete m_pFindDlg;
	}
}

LONG CHEditView::OnFindReplace(WPARAM wParam, LPARAM lParam)
{
	if (m_pFindDlg == NULL)
	{
		if (!FindText(m_strFindText, m_dwFindMask))
		{
			OnTextNotFound(m_strFindText);
		}

		SetFocus();
	}
	else if (!m_pFindDlg->IsTerminating())
	{
		m_strFindText = m_pFindDlg->GetFindString();
		m_dwFindMask = (m_pFindDlg->SearchDown()? FR_DOWN: 0) |
			           (m_pFindDlg->MatchCase()? FR_MATCHCASE: 0) |
		               (m_pFindDlg->MatchWholeWord()? FR_WHOLEWORD: 0);
		
		if (!FindText(m_strFindText,m_dwFindMask))
		{
			OnTextNotFound(m_strFindText);
		}

		if (m_pFindDlg != NULL)
		{
			m_pFindDlg->SetFocus ();
		}
	}

	AfxGetMainWnd()->ModifyStyle(WS_DISABLED, 0);	//thinkry 2004-12-08
	return 0;
}

BOOL CHEditView::FindText (const CString& strFind, DWORD dwFindMask)
{
	if (m_pWndEditCtrl == NULL)
	{
		return FALSE;
	}

	ASSERT_VALID (m_pWndEditCtrl);

	return m_pWndEditCtrl->FindText(strFind,
						(dwFindMask & FR_DOWN) != 0,
						(dwFindMask & FR_MATCHCASE) != 0,
						(dwFindMask & FR_WHOLEWORD) != 0);
}

LONG CHEditView::OnEditChange(WPARAM, LPARAM)
{
	ASSERT_VALID(this);
//	GetDocument()->SetModifiedFlag();
	ASSERT_VALID(this);

	return 0;
}

